package com.swz.dcrm.ui.viewmodel;

import android.util.Log;

import androidx.lifecycle.MediatorLiveData;

import com.swz.dcrm.api.DcrmApi;
import com.swz.dcrm.model.Approval;
import com.swz.dcrm.model.BaseResponse;
import com.swz.dcrm.model.PageResponse;
import com.swz.dcrm.model.Tab;
import com.xh.baselibrary.callback.BaseViewModel;
import com.xh.baselibrary.callback.CallBack;

import java.util.List;

import retrofit2.Retrofit;

public class ApprovalViewModel extends BaseViewModel {
    private Retrofit mRetrofit;
    private DcrmApi dcrmApi;
    private MediatorLiveData<BaseResponse<List<Tab>>> approvalTabsHasHanded;
    private MediatorLiveData<BaseResponse<List<Tab>>> approvalTabsHasNotHanded;
    private MediatorLiveData<PageResponse<Approval>> approvalsType1 = new MediatorLiveData<>();
    private MediatorLiveData<BaseResponse<String>> confirmApprovalOpinion;
    private MediatorLiveData<BaseResponse<String>> applyDealClue;
    private MediatorLiveData<BaseResponse<String>> applyCancelOrder;

    public ApprovalViewModel(Retrofit retrofit) {
        this.mRetrofit = retrofit;
        dcrmApi = mRetrofit.create(DcrmApi.class);
    }

    public MediatorLiveData<BaseResponse<List<Tab>>> getApprovalTabs(boolean isHandled) {
        if (isHandled) {
            approvalTabsHasHanded = creatLiveData(approvalTabsHasHanded);
            dcrmApi.getApprovalTabList(isHandled).enqueue(new CallBack<>(this, approvalTabsHasHanded));
            return approvalTabsHasHanded;
        } else {
            approvalTabsHasNotHanded = creatLiveData(approvalTabsHasNotHanded);
            dcrmApi.getApprovalTabList(isHandled).enqueue(new CallBack<>(this, approvalTabsHasNotHanded));
            return approvalTabsHasNotHanded;
        }

    }


    public MediatorLiveData<PageResponse<Approval>> getApprovals(Integer type, int page, int size, boolean isDeal) {
        Boolean b = null;
        Boolean b1 = null;
        if (isDeal) {
            b1 = true;
        } else {
            b = true;
        }
        dcrmApi.getApprovalList(page, size, type, b, b1).enqueue(new CallBack<>(this, approvalsType1));
        return approvalsType1;


    }

    public MediatorLiveData<BaseResponse<List<Tab>>> getApprovalTabsResult(boolean isHandled) {
        if (isHandled) {
            approvalTabsHasHanded = creatLiveData(approvalTabsHasHanded);
            return approvalTabsHasHanded;
        } else {
            approvalTabsHasNotHanded = creatLiveData(approvalTabsHasNotHanded);
            return approvalTabsHasNotHanded;
        }

    }

    public MediatorLiveData<BaseResponse<String>> confirmApprovalOpinion(List<Long> ids, boolean pass, String remark) {
        confirmApprovalOpinion = creatLiveData(confirmApprovalOpinion);
        int i = 0;
        if (pass) {
            i = 1;
        }
        dcrmApi.confirmApprovalOpinion(ids, i, remark).enqueue(new CallBack<>(this, confirmApprovalOpinion));
        return confirmApprovalOpinion;
    }

    public MediatorLiveData<BaseResponse<String>> applyDefeatedClue(long clueId, boolean defeatedHasBuyCar, String reason) {
        applyDealClue = creatLiveData(applyDealClue);
        dcrmApi.applyDefeatedClue(clueId, defeatedHasBuyCar, reason).enqueue(new CallBack<>(this, applyDealClue));
        return applyDealClue;
    }

    public MediatorLiveData<BaseResponse<String>> applyHandUpClue(long clueId, String reason) {
        applyDealClue = creatLiveData(applyDealClue);
        dcrmApi.applyHandUpClue(clueId, reason).enqueue(new CallBack<>(this, applyDealClue));
        return applyDealClue;
    }

    public MediatorLiveData<BaseResponse<String>> getApplyDefeatedClue() {
        applyDealClue = creatLiveData(applyDealClue);
        return applyDealClue;
    }

    public MediatorLiveData<BaseResponse<String>> applyCancelOrder(long orderId, String reason) {
        applyCancelOrder = creatLiveData(applyCancelOrder);
        applyCancelOrder.setValue(null);
        dcrmApi.applyCancelOrder(orderId, reason).enqueue(new CallBack<>(this, applyCancelOrder));
        return applyCancelOrder;
    }


}
